package com.liinxin.flac_management.page;

import com.liinxin.flac_management.controller.SongLabelController;
import com.liinxin.flac_management.entity.FileTextField;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;

public class AddCoverUI {
    private Stage primaryStage;
    private Scene startScene;

    private SongLabelController songLabelController = new SongLabelController();

    public AddCoverUI() {
    }

    public AddCoverUI(Stage primaryStage, Scene startScene) {
        this.primaryStage = primaryStage;
        this.startScene = startScene;
    }

    public Scene getScene() {
        // 创建一个标签
        Label label = new Label("Flac文件添加封面");
        label.setFont(new Font("Arial", 20));

        // 输入框设置
        Label workLabel = new Label("请输入作品地址: ");
        workLabel.setFont(new Font("Arial", 20));
        FileTextField workPathInput = new FileTextField();
        workPathInput.setFont(new Font("Arial",20));

        Label imgLabel = new Label("请输入图片地址: ");
        imgLabel.setFont(new Font("Arial", 20));
        FileTextField imgPathInput = new FileTextField();
        imgPathInput.setFont(new Font("Arial",20));

        // 添加图片按钮
        Button addButton = new Button("添加封面");
        addButton.setFont(new Font("Arial", 20));
        // 清空地址按钮
        Button clearButton = new Button("清空地址");
        clearButton.setFont(new Font("Arial", 20));
        // 创建一个返回按钮
        Button backButton = new Button("返回主页");
        backButton.setFont(new Font("Arial", 20));

        // 创建hbox管理按钮
        HBox buttonBox = new HBox(40);
        buttonBox.setAlignment(Pos.CENTER);
        // 将按钮添加到HBox布局管理器中
        buttonBox.getChildren().addAll(addButton, clearButton, backButton);

        // 添加事件
        // 添加图片
        addButton.setOnAction(event -> {
            System.out.println("添加封面");
            System.out.println("作品地址: "+ workPathInput.getText());
            System.out.println("图片地址: "+ imgPathInput.getText());

            try {
                songLabelController.addCover(workPathInput.getText(), imgPathInput.getText());
                Alert alert = new Alert(Alert.AlertType.WARNING);
                alert.setTitle("成功");
                alert.setHeaderText("添加成功");
                // 显示提示框并等待用户关闭
                alert.showAndWait();

            } catch (Exception e) {
                // 显示错误提示框
                Alert alert = new Alert(Alert.AlertType.ERROR);
                alert.setTitle("错误");
                alert.setHeaderText("路径错误");
                // 显示提示框并等待用户关闭
                alert.showAndWait();
                // 清空输入框数据
                workPathInput.setText("");
                imgPathInput.setText("");
            }

        });
        // 清空地址
        clearButton.setOnAction(event -> {
            System.out.println("清空地址");
            workPathInput.setText("");
            imgPathInput.setText("");

        });
        // 返回主页
        backButton.setOnAction(event -> {
            System.out.println("返回主页");
            primaryStage.setScene(startScene);
            primaryStage.show();
        });

        // 创建一个VBox布局管理器作为根布局
        VBox root = new VBox(15);
        root.getChildren().addAll(label, workLabel, workPathInput, imgLabel, imgPathInput,buttonBox);

        // 创建一个场景并返回
        Scene scene = new Scene(root, 500, 500);
        return scene;
    }
}

